package particleField;

import javax.media.opengl.GL;

import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;
import processing.opengl.PGraphicsOpenGL;

public class ToPoint01 extends PApplet{
	
	ParticleSystem ps;
	int mouseX_old;
	int mouseY_old;
	int width = 1024;
	int height = 768;
	PImage newImg; 
	boolean mod;
//	PGraphicsOpenGL gl;

	public void setup() {
		size(width, height, OPENGL);
		
//		PGraphicsOpenGL gl = (PGraphicsOpenGL) g;
		
		colorMode(RGB, 255, 255, 255, 100);
		ps = new ParticleSystem(1, new PVector(width,height), this);
	//		smooth();
		//hint(ENABLE_OPENGL_4X_SMOOTH);
		mouseX_old = height/2;
		mouseY_old = width/2;
		ps.force_disable_home();
		fill( 15,0);
		stroke(0);
		ellipse(mouseX, mouseY, 15, 15);
		background(0x00000000);
		ps.loadDirectionPic(this);
	}
	


	public void draw() {

//		if(mousePressed){
//			if(!mod){
//				ps.force_home();
//				mod = true;
//				ps.resetMagField();
//			}	
//		}else{
//			ps.force_disable_home();
//			if(mod){
//				ps.resetMagField();
//			}
//			mod = false;
//		}
		
//		if(frameCount%1000 == 0) background(0x00FFFFFF);
		background(0x00000000);
		
		if(mousePressed){
			ps.gravity();
		}
		
		if(mouseX_old != mouseX && mouseY_old != mouseY){
			int radius = (int) new PVector(mouseX_old, mouseY_old).dist( new PVector(mouseX, mouseY));
			
			if(radius < 4000){
//				ps.add_magnet(this, new PVector(mouseX, mouseY), 70, true);
				ps.addForceDirection( new PVector(mouseX, mouseY)/*, new PVector(mouseX_old, mouseY_old) */);
//				ps.add_magnet(this, new PVector(mouseX, mouseY), 30, false);

			}
			
		}
		mouseX_old = mouseX;
		mouseY_old = mouseY;
			
		ps.run(this);
//		ps.showDirection(this);
		System.out.println("FPS:" + this.frameRate);
		
//		if (mouseButton == LEFT){
//			ps.add_magnet(new PVector(width/2-300, height/2), 100, false);
//		}else if(mouseButton == RIGHT){
//			ps.add_magnet(new PVector(width/2, height/2), 100, true);
//		}

//		ps.add_magnet(this, new PVector(300, 300), 100, false);
//		ps.add_magneticBox(this, 20);
		
		
//		for (int i = 0; i < 1; i++) {	
//			ps.addParticle(mouseX,mouseY);	
//		}
		
		
//		ellipse(50, 50, 50, 50);
//		newImg = fastblur( this.get() );
//		image(newImg, 0, 0);
		
//		filter(BLUR, 1.0F);
		
//		newImg = loadImage("ellipse_blur.png");
//		image(newImg, 30, 30);
		
//		text(frameRate, 10, 10);

	}
	
	PImage fastblur(PImage img) {
		
		for (int x = 1; x < height-1; x++) {
			for (int y = 1; y < width-1; y++) {
				int newVal_g = img.get(x-1, y-1)&0xFF + img.get(x-1, y)&0xFF + img.get(x-1, y+1)&0xFF
						+ img.get(x, y-1)&0xFF + img.get(x, y)&0xFF + img.get(x, y+1)&0xFF
						+ img.get(x+1, y-1)&0xFF + img.get(x+1, y)&0xFF + img.get(x+1, y+1)&0xFF;
				newVal_g = newVal_g/9;
				img.set(x, y, newVal_g);
				
			}			
		}
		return img;
	}

}
